package as3.component.manager
{
	import as3.component.ui.DragBitmap;
	
	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	
	public class ElementDragManager
	{
		
		private static var instance:ElementDragManager;
		
		private var stage:Stage;
		
		private var dragImg:DragBitmap;
		private var elementData:Object;
		private var offPoint:Point;
		
		public function ElementDragManager()
		{
			if (!instance) return;
		}
		
		public static function getInstance():ElementDragManager
		{
			if (!instance) instance = new ElementDragManager();
			
			return instance;
		}
		
		public function runAt(s:Stage):void
		{
			stage = s;
		}
		
		public function beginDrag(target:DisplayObject, data:Object, offP:Point = null):void
		{
			clearDrag();
			if (!target || !data) return;
			elementData = data;
			if (offP) {
				offPoint = offP;
			} else {
				offPoint = new Point(target.mouseX, target.mouseY);
			}
			dragImg = new DragBitmap();
			dragImg.drawBitmap(target);
			PopupManager.getInstance().addPopUp(dragImg, new Point(stage.mouseX - offPoint.x, stage.mouseY - offPoint.y));
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onStageMove);
		}
		
		public function clearDrag():void
		{
			if (dragImg) PopupManager.getInstance().removePopUp(dragImg);
			dragImg = null;
			elementData = null;
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, onStageMove);
		}
		
		private function onStageMove(evt:MouseEvent):void
		{
			dragImg.x = stage.mouseX - offPoint.x;
			dragImg.y = stage.mouseY - offPoint.y;
			evt.updateAfterEvent();
		}
		
		public function get dragTarget():DragBitmap
		{
			return dragImg
		}
		
		public function get dragProxy():Object
		{
			return elementData;
		}
		
		public static function checkDropTarget(target:DisplayObject):Class
		{
			var b:Boolean = true;
			var return_class:Class;
			
			return return_class;
		}

	}
}